package com.clickpaas.materialsprocurement.utils;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.PMSpurchaseApplyLineDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DataInfoUtil {

    /**
     * 判空转换为默认值
     * @param tagert 目标值
     * @param defaltValue 默认值
     * @return
     */
    public static String setDefaltValue(String tagert,String defaltValue){
        if(StringUtils.isBlank(tagert)){
            return defaltValue;
        }
        return tagert;
    }

    /**
     * string时间转整型
     * @param date
     * @return
     * @throws ParseException
     */
    private static Long stringDate2Int(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime= null;
        try {
            dateTime = format.parse(date);
        } catch (ParseException e) {
            log.info("时间转换失败：【{}】",e);
        }
        long timeLong =dateTime.getTime();
        return timeLong;
    }

    /**
     * project对象降序排列
     */
    public static void sortProject(List<PMSpurchaseApplyLineDTO> Datas){
        Collections.sort(Datas, new Comparator<PMSpurchaseApplyLineDTO>() {

            @Override
            public int compare(PMSpurchaseApplyLineDTO Data1, PMSpurchaseApplyLineDTO Data2) {
                int i = DataInfoUtil.stringDate2Int(Data2.getCreatedTime()).intValue() - DataInfoUtil.stringDate2Int(Data1.getCreatedTime()).intValue();
                if(i == 0){
                    return Integer.valueOf(Data2.getId()) - Integer.valueOf(Data1.getId());
                }
                return i;
            }
        });
    }

    /**
     * 对象属性判空
     * @param obj
     * @return
     * @throws Exception
     */
    public static boolean isAllFieldNull(Object obj) throws Exception{
        Class stuCla = (Class) obj.getClass();// 得到类对象
        Field[] fs = stuCla.getDeclaredFields();//得到属性集合
        boolean flag = true;
        for (Field f : fs) {
            /**
             * 设置属性是可以访问的(私有的也可以)
             */
            f.setAccessible(true);
            /**
             * 得到此属性的值
             */
            Object val = f.get(obj);
            /**
             * 只要有1个属性不为空,那么就不是所有的属性值都为空
             */
            if(val!=null && StringUtils.isNotBlank(val.toString())) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * @company <上海爱湃斯科技有限公司>
     * @email <zhihui.peng@clickpaas.com>
     * @author huiye
     * @decription: 对象复制
     * @date 2021/2/1 2:13 下午
     */
    public static <T> List<T> castListCopy(Object obj, Class<?> clazz, Class<T> reClazz) {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>) {
            List listsize = (List<?>)obj;
            //多线程处理数据
            ExecutorService pool = Executors.newCachedThreadPool();
            CountDownLatch latch = new CountDownLatch(listsize.size());
            for (Object o : (List<?>) obj) {
                //开启子线程
                Runnable run = new Runnable() {
                    public void run() {
                        try {
                            T entity = reClazz.newInstance();
                            BeanUtils.copyProperties(clazz.cast(o),entity);
                            result.add(entity);
                        }catch(Exception e){
                            log.info("多线程转换报错,请检查：【{}】",e);
                        } finally {
                            latch.countDown();
                        }
                    }
                };
                pool.execute(run);
            }try {
                latch.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                log.info("多线程转换报错,请检查：【{}】",e);
            }finally {
                pool.shutdown();
            }
            return result;
        }
        return null;
    }

}
